﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AmorCanino.Web.Models.Services
{
    public class AdoptionService : BaseService, IAdoptionService
    {
        public List<Models.Pet> getPagedPets(int pageIndex)
        {
            var db = new AmorCaninoDBDataContext();
            var records_by_page = AmorCanino.Common.ConfigurationHandler.GetAdoptedRecordsByPage;

            int totalPages = db.Animals.Where(a => a.Visible && a.IsForAdoption).OrderByDescending(a => a.AnimalId).Count();

            if (totalPages / records_by_page < pageIndex)
            {
                return db.Animals.Where(a => a.Visible && a.IsForAdoption)
                    .OrderByDescending(a => a.AnimalId)
                        .Select(a => new Models.Pet()
                        {
                            BirthDate = a.BirthDate,
                            PetId = a.AnimalId,
                            PetName = a.Name,
                            RaceId = a.RaceId,
                            ReceName = a.Race.RaceName,
                            UserId = a.UserId
                        }).ToList();
            }

            return db.Animals.Where(a => a.Visible && a.IsForAdoption)
                .OrderByDescending(a => a.AnimalId)
                    .Skip((pageIndex - 1) * records_by_page)
                        .Take(records_by_page)
                            .Select(a => new Models.Pet() {
                                BirthDate = a.BirthDate,
                                PetId = a.AnimalId,
                                PetName = a.Name,
                                RaceId = a.RaceId,
                                ReceName = a.Race.RaceName,
                                UserId = a.UserId
                        }).ToList();
        }

        public int getTotalPages()
        {
            var records_by_page = AmorCanino.Common.ConfigurationHandler.GetAdoptedRecordsByPage;

            return new AmorCaninoDBDataContext().Animals.Where(a => a.Visible && a.IsForAdoption).OrderByDescending(a => a.AnimalId).Count() / records_by_page;
        }
    }
}
